﻿using System;
using System.Runtime.Serialization;

namespace Simp.Model.Exceptions
{
	/// <summary>Base exception holding a function name.</summary>
	[Serializable()]
	public class FunctionExceptionBase : SimpRuntimeException
	{
		private const string FUNCTION_NAME = "FunctionName";

		private readonly string _functionName;

		/// <summary>Constructor taking only the function name.</summary>
		/// <param name="functionName"/>
		public FunctionExceptionBase(string functionName)
			: base(string.Format("Unknown Function:  {0}", functionName))
		{
			_functionName = functionName;
		}

		/// <summary>Constructor taking the function name and a message.</summary>
		/// <param name="message"/>
		/// <param name="functionName"/>
		public FunctionExceptionBase(string message, string functionName)
			: base(message)
		{
			_functionName = functionName;
		}

		/// <summary>Constructor taking the function name, a message and an inner exception.</summary>
		/// <param name="message"></param>
		/// <param name="inner"></param>
		/// <param name="functionName"/>
		public FunctionExceptionBase(string message, Exception inner, string functionName)
			: base(message, inner)
		{
			_functionName = functionName;
		}

		/// <summary>Constructor needed for serialization when exception propagates from a remoting server to the client.</summary>
		/// <param name="info"></param>
		/// <param name="context"></param>
		protected FunctionExceptionBase(SerializationInfo info, StreamingContext context)
			: base(info, context)
		{
			_functionName = info.GetString(FUNCTION_NAME);
		}

		/// <summary>Serializes custom content.</summary>
		/// <param name="info"></param>
		/// <param name="context"></param>
		public override void GetObjectData(SerializationInfo info, StreamingContext context)
		{
			base.GetObjectData(info, context);

			info.AddValue(FUNCTION_NAME, _functionName);
		}

		/// <summary>Returns the name of the function.</summary>
		public string FunctionName
		{
			get { return _functionName; }
		}
	}
}